package com.legal.aid.system.service.impl;

import java.util.List;
import java.util.Random;

import com.legal.aid.common.exception.ServiceException;
import com.legal.aid.common.utils.DateUtils;
import com.legal.aid.common.core.domain.entity.CommonUser;
import com.legal.aid.system.domain.Lawyer;
import com.legal.aid.system.domain.vo.OrderStatisticsVO;
import com.legal.aid.system.mapper.LawyerMapper;
import com.legal.aid.system.mapper.SysCommonUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.legal.aid.system.mapper.OrderMapper;
import com.legal.aid.system.domain.Order;
import com.legal.aid.system.service.IOrderService;

/**
 * 订单Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-27
 */
@Service
public class OrderServiceImpl implements IOrderService 
{
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private SysCommonUserMapper commonUserMapper;

    @Autowired
    private LawyerMapper lawyerMapper;

    /**
     * 统计订单
     * @return
     */
    @Override
    public OrderStatisticsVO getOrderStatistics() {

        return orderMapper.getOrderStatistics();
    }

    /**
     * 查询订单
     * 
     * @param id 订单主键
     * @return 订单
     */
    @Override
    public Order selectOrderById(Long id)
    {
        return orderMapper.selectOrderById(id);
    }

    /**
     * 查询订单列表
     * 
     * @param order 订单
     * @return 订单
     */
    @Override
    public List<Order> selectOrderList(Order order)
    {
        return orderMapper.selectOrderList(order);
    }

    /**
     * 新增订单
     * 
     * @param order 订单
     * @return 结果
     */
    @Override
    public int insertOrder(Order order)
    {
        if(order.getUserId() == null||order.getUserId().equals("")||order.getLawyerId()==null||order.getLawyerId().equals("")){
            throw new ServiceException("userid or lawyerid is null");
        }

        order.setCreateTime(DateUtils.getNowDate());
        //订单编号
        String orderNo = generateOrderNumber(order.getUserId());
        order.setOrderNo(orderNo);

        //获取用户信息
        CommonUser commonUser = commonUserMapper.selectCommonUserById(order.getUserId());
        order.setUserName(commonUser.getUserName());
        order.setPhone(commonUser.getPhoneNumber());
        order.setAddress(commonUser.getContactAddress());
        order.setCreatedBy(commonUser.getUserName());
        //获取价格
        Lawyer lawyer = lawyerMapper.selectLawyerByLawyerId(Long.valueOf(order.getLawyerId()));
        order.setPrice(lawyer.getConsultationPrice());
        return orderMapper.insertOrder(order);
    }

    /**
     * 订单编号生成   当前时间戳后8位，随机数4位，用户ID后4位
     * @param buyerId 买家ID
     * @return 订单编号
     */
    private String generateOrderNumber(long buyerId) {
        // 获取当前时间戳（毫秒）
        long timestamp = System.currentTimeMillis();

        // 生成4位随机数
        Random random = new Random();
        int randomNum = random.nextInt(10000);

        // 处理买家ID，取后4位，不足补0
        String buyerIdStr = String.format("%04d", buyerId % 10000);

        // 组合：时间戳后8位 + 随机数4位 + 用户ID后4位
        String timestampStr = String.valueOf(timestamp).substring(5); // 取后8位
        String randomStr = String.format("%04d", randomNum);

        return timestampStr + randomStr + buyerIdStr;
    }

    /**
     * 修改订单
     * 
     * @param order 订单
     * @return 结果
     */
    @Override
    public int updateOrder(Order order)
    {
        order.setUpdateTime(DateUtils.getNowDate());
        return orderMapper.updateOrder(order);
    }

    /**
     * 批量删除订单
     * 
     * @param ids 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteOrderByIds(Long[] ids)
    {
        return orderMapper.deleteOrderByIds(ids);
    }

    /**
     * 删除订单信息
     * 
     * @param id 订单主键
     * @return 结果
     */
    @Override
    public int deleteOrderById(Long id)
    {
        return orderMapper.deleteOrderById(id);
    }
}
